﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class BackGroundCreater : MonoBehaviour {

	public Transform bigBackgroundTransform,litteBackgroundTransform;
	/// <summary>
	/// 大背景
	/// </summary>
	public Background bigBackground;
	/// <summary>
	/// 小背景
	/// </summary>
	public Background litteBackground;

	public float speed = 2f;

	private List<Sprite> bigBackgroundSprites;

	private List<Sprite> litteBackgroundSprites;

	private Vector2 canPos;

	void Update(){
		CameraFollow();
	}

	void CameraFollow(){
		float bx = ( Camera.main.transform.position.x - canPos.x ) / speed;
		float by = ( Camera.main.transform.position.y - canPos.y ) / speed;
		bigBackgroundTransform.position = new Vector3(bx,by,bigBackgroundTransform.position.z);
		float lx = ( canPos.x - Camera.main.transform.position.x ) / speed;
		float ly = ( canPos.y - Camera.main.transform.position.y ) / speed;
		litteBackgroundTransform.position = new Vector3(lx,ly,litteBackgroundTransform.position.z);
	}

	void InintBackground( Vector2 center ){
		LoadAllSprites();
		LoadBigBackground();
		LoadLitteBackground();
		bigBackgroundTransform.position = center;
		litteBackgroundTransform.position = center;
		canPos.x = Camera.main.transform.position.x;
		canPos.y = Camera.main.transform.position.y;
	}

	void LoadAllSprites(){
		//清空缓存数据
		if(bigBackgroundSprites == null){
			bigBackgroundSprites = new List<Sprite>();
		}else{
			bigBackgroundSprites.Clear();
		}
		if(litteBackgroundSprites == null){
			litteBackgroundSprites = new List<Sprite>();
		}else{
			litteBackgroundSprites.Clear();
		}
		if(bigBackground.isMutilSprite && litteBackground.isMutilSprite && bigBackground.multilTextrueName == litteBackground.multilTextrueName){
			
			Sprite[] sprites= Resources.LoadAll<Sprite>(@"Backgrounds"+ System.IO.Path.DirectorySeparatorChar+bigBackground.multilTextrueName);
			foreach(Sprite sprite in sprites){
				if(sprite.name.StartsWith(bigBackground.firstName)){
					bigBackgroundSprites.Add(sprite);
				}else if(sprite.name.StartsWith(litteBackground.firstName)){
					litteBackgroundSprites.Add(sprite);
				}
			}
		}else{
			//加载大背景数据
			if(bigBackground.isMutilSprite){
				Sprite[] sprites= Resources.LoadAll<Sprite>(@"Backgrounds"+ System.IO.Path.DirectorySeparatorChar+bigBackground.multilTextrueName);
				foreach(Sprite sprite in sprites){
					if(sprite.name.StartsWith(bigBackground.firstName)){
						bigBackgroundSprites.Add(sprite);
					}
				}
			}else{
				Sprite[] sprites= Resources.LoadAll<Sprite>(@"Backgrounds"+ System.IO.Path.DirectorySeparatorChar+bigBackground.multilTextrueName);
				foreach(Sprite sprite in sprites){
					bigBackgroundSprites.Add(sprite);
				}
			}
			//加载小背景
			if(litteBackground.isMutilSprite){
				Sprite[] sprites= Resources.LoadAll<Sprite>(@"Backgrounds"+ System.IO.Path.DirectorySeparatorChar+litteBackground.multilTextrueName);
				foreach(Sprite sprite in sprites){
					if(sprite.name.StartsWith(litteBackground.firstName)){
						bigBackgroundSprites.Add(sprite);
					}
				}
			}else{
				Sprite[] sprites= Resources.LoadAll<Sprite>(@"Backgrounds"+ System.IO.Path.DirectorySeparatorChar+litteBackground.multilTextrueName);
				foreach(Sprite sprite in sprites){
					litteBackgroundSprites.Add(sprite);
				}
			}
		}
	}

	void LoadBigBackground(){
		//初始化大背景
		int randomBg = Random.Range(0,bigBackgroundSprites.Count);
		GameObject bigBg = new GameObject();
		bigBg.name = "big_background";
		SpriteRenderer sr= bigBg.AddComponent<SpriteRenderer>();
		sr.sprite = bigBackgroundSprites[randomBg];
		sr.sortingLayerName = "Background";
		sr.sortingOrder = Random.Range(-1,0);
		bigBg.transform.localScale = bigBackground.scale;
		bigBg.transform.SetParent(bigBackgroundTransform);
	}

	void LoadLitteBackground(){
		//初始化小背景
		int showNum = Random.Range(1,litteBackground.maxShow + 1);
		for(int i=0 ; i < showNum ; i++){
			int randomLBg = Random.Range(0,bigBackgroundSprites.Count);
			GameObject ltBg = new GameObject();
			ltBg.name = "lit_background"+i;
			SpriteRenderer ltsr= ltBg.AddComponent<SpriteRenderer>();
			ltsr.sprite = litteBackgroundSprites[randomLBg];;
			ltsr.sortingLayerName = "Background";
			ltsr.sortingOrder = Random.Range(0,2);
			ltsr.color = new Color(ltsr.color.r,ltsr.color.g,ltsr.color.b,Random.Range(0.1f,0.8f));;
			ltBg.transform.localScale = litteBackground.scale;
			ltBg.transform.SetParent(litteBackgroundTransform);
		}
	}


	[System.Serializable]
	public class Background{
		public string firstName;
		public bool isMutilSprite;
		public string multilTextrueName;
		public int maxShow;
		public Vector3 scale = new Vector3(1,1,1);
	}
}
